﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data;

namespace SPMigrationOracleUtility
{
    class cls_mapping
    {
        /// <summary>
        /// Local Variable declarations
        /// </summary>
        DataTable dtResults;
        private string strConResult;
        private string strSelectedItem;
        public int cnt;
        private string DataSource="127.0.0.1";
        private string UserId = "hr";
        private string Password = "pass@word1";
        //private string UserId = "jtc";
        //private string Password = "tiger";
        

        public object Lst_Tbl_Rec(RadioButton rdnTbl, Button btnForward, ListBox li, ListBox l2, bool flag1)
        {
            
            if (rdnTbl.Checked && flag1== false)
            {
                l2.Items.Clear();
                li.Items.Clear();
                btnForward.Enabled = true;
               
                    ShowAllTables(li);
               
                    flag1 = false;
                return li;
            }
            else
            {
                return null;
            }
        }

        public object Lst_View_Rec(RadioButton rdnView, Button btnForward, ListBox li, ListBox l2,bool flag1)
        {

            if (rdnView.Checked && flag1 == false)
            {
                l2.Items.Clear();
                li.Items.Clear();
                btnForward.Enabled = true;
                ShowAllViews(li);
                flag1 = false;
                return li;
            }
            else
            {
                return null;
            }
        }

        public object Btn_DbForward(RadioButton rd,ListBox lstDBs, ListBox lstDB, Button btnFrd, Button btnBrd, Button btnNextPnl3)
        {
            string str = rd.Name;

            cnt = lstDB.Items.Count;

            if (str == "rdBtnTables")
            {
                    if (cnt == 2)
                    {
                        //MessageBox("Cannot add more than 2 items on the list");
                    }

                    else
                    {
   
                        if (lstDBs.SelectedItems.Count == 0)
                        {
                            MessageBox.Show("Please select the list");
                        }
                        else
                        {
                            btnFrd.Enabled = true;
                            lstDB.Items.Add(lstDBs.SelectedItem.ToString());
                            int selectedIndex = Convert.ToInt32(lstDBs.SelectedIndex);
                            lstDBs.Items.RemoveAt(selectedIndex);
                            cnt++;
                        }

                    }

                    if (cnt == 2)
                    {
                        btnFrd.Enabled = false;
                        btnBrd.Enabled = true;
                    }

                    if (cnt == 1)
                    {
                       btnNextPnl3.Enabled = true;
                       btnBrd.Enabled = true;
                    }
 
            }

            else
                 {
                    if (cnt > 1)
                    {
                        //MessageBox("Cannot add more than 2 items on the list");
                    }

                    else
                    {
                        btnFrd.Enabled = true;
                        if (lstDBs.SelectedItems.Count == 0)
                        {
                            MessageBox.Show("Please select the list");
                        }
                        else
                        {
                            lstDB.Items.Add(lstDBs.SelectedItem.ToString());
                            int selectedIndex = Convert.ToInt32(lstDBs.SelectedIndex);
                            lstDBs.Items.RemoveAt(selectedIndex);
                            cnt++;
                        }

                    }

                    if (cnt == 1)
                    {
                        btnFrd.Enabled = false;
                        btnBrd.Enabled = true;
                        btnNextPnl3.Enabled = true;
                    }
                    else
                    {
                        btnFrd.Enabled = true;
                    }
                }
           
            return lstDB;
        }

        public object Btn_DbBackward(ListBox lstDBs, ListBox lstDB, Button btnFrd,Button btnBak, Button btnNextPnl2)
        {
            cnt = lstDB.Items.Count;
            if (cnt == 0)
            {
                if (lstDB.SelectedItems.Count == 0)
                {
                    //MessageBox.Show("Please select the list");
                }
                else
                {
                    btnFrd.Enabled = false;
                }
                
            }
            else
            {
                if (lstDB.SelectedItems.Count == 0)
                {
                    MessageBox.Show("Please select the list");
                }
                else
                {
                    btnFrd.Enabled = true;
                    lstDBs.Items.Add(lstDB.SelectedItem.ToString());
                    int selectedIndex = Convert.ToInt32(lstDB.SelectedIndex);
                    lstDB.Items.RemoveAt(selectedIndex);
                    cnt--;
                }
            }

            if (cnt == 0)
            {
               
                btnBak.Enabled = false;
                btnNextPnl2.Enabled = false;
            }

         
            return lstDBs;
        }

        public object Panel_Prop(Panel pnl)
        {
            int x = 21;
            int y = 40;
            pnl.Location = new Point(x, y);
            return pnl;

        }

        public void ShowAllTables(ListBox listTables)
        {

            strConResult = orcConnection.Connect(DataSource, UserId, Password);

            if (strConResult.Length <= 0)
            {
               
                dtResults = orcConnection.GetUserTables();

                for (int cUserTbls = 0; cUserTbls < dtResults.Rows.Count; cUserTbls++)
                {
                    listTables.Items.Add(dtResults.Rows[cUserTbls][0].ToString());
                }
            }
            else
            {
               
                MessageBox.Show(strConResult);
            }
        }

        public void ShowAllViews(ListBox listViews)
        {
            strConResult = orcConnection.Connect(DataSource, UserId, Password);
   
            if (strConResult.Length <= 0)
            {
                dtResults = orcConnection.GetUserViews();
                listViews.Items.Clear();
                
                for (int cUserViews = 0; cUserViews < dtResults.Rows.Count; cUserViews++)
                {
                    listViews.Items.Add(dtResults.Rows[cUserViews][0].ToString());
                }
            }
            else
            {
                MessageBox.Show(strConResult);
            }
        }

        public void LoadOrcData(string qry, DataGridView gv)
        {
            if (qry == "")
            {
                MessageBox.Show("Please enter an SQL statement.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {

                strConResult = orcConnection.Connect(DataSource, UserId, Password);
                try 
                {
                    if (strConResult.Length <= 0)
                    {
                        dtResults = orcConnection.ExecuteQry(qry);
                        gv.DataSource = dtResults; // Populate the DataGridView control, to reflect the Sql query.
                    }

                    else
                    {
                        MessageBox.Show(strConResult);
                    }
                }
                catch(Exception ee)
                {
                    gv.DataSource = null;
                    MessageBox.Show(ee.Message );
                } 
             }
         }
 
        public void LoadOrcData_Ck(string UserQry, ListBox lstTbl, DataGridView gvBind)
        {
            LoadOrcData(UserQry, gvBind);
        }

        public enum ObjectState
        {
            Structure,
            Data
        }

        public void LoadObjectData(string objState, ComboBox CmbSrcTableName, ComboBox CmbSrcFieldName)
        {
            CmbSrcFieldName.DisplayMember = "fieldName";
            try
            {
                // Connect to the database using the login details.
                strConResult = orcConnection.Connect(DataSource, UserId, Password);

                // Check if an error occured.
                if (strConResult.Length <= 0)
                {
                    // Get the selected table/view name.
                    strSelectedItem = Convert.ToString(CmbSrcTableName.SelectedItem);
                    // What is the current state. If the Structure button was clicked, then view the structure of
                    // the table/view
                    if (objState == "Structure")
                    {
                        dtResults = orcConnection.GetObjectStructure(strSelectedItem);
                        //dGrid.DataSource = dtResults;
                    }

                    // If the Data button was clicked, view the data of the table.
                    if (objState == "Data")
                    {
                        dtResults = orcConnection.GetObjectData(strSelectedItem);
                        //dGrid.DataSource = dtResults;
                        foreach (DataColumn col in dtResults.Columns)
                        {
                            CmbSrcFieldName.Items.Add(new SourceFieldType { fieldName = col.ColumnName, fieldType = Convert.ToString(col.DataType) });
                        }
                    }
                }
                else
                {
                    MessageBox.Show(strConResult);
                }

                if (CmbSrcFieldName.Items.Count > 0)
                    CmbSrcFieldName.SelectedIndex = 0;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        public void LoadObjectType(string objState, ComboBox CmbSrcFieldName, Label lblSrcFieldName)
        {
            SourceFieldType objfld = (SourceFieldType)CmbSrcFieldName.SelectedItem;
            lblSrcFieldName.Text = objfld.fieldType;
        }

       
    }
}
